package com.mercycrazy.recruit.support.jpa;

import cn.hutool.core.lang.Snowflake;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;

import java.io.Serializable;

/**
 * 自定义 JPA ID 生成器: 适配雪花算法
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @see cn.hutool.core.lang.Snowflake
 * @since 2023-01-14
 */
public class SnowIdGenerator implements IdentifierGenerator {

    @Override
    public Serializable generate(
            SharedSessionContractImplementor sharedSessionContractImplementor, Object o
    ) throws HibernateException {

        return new Snowflake().nextIdStr();
    }
}
